<template>
	<view>
		<view class="cu-form-group">
			<view class="title">{{i18n.msg.title2}}</view>
			<input class="text" :placeholder="i18n.msg.inputtitle2" v-model="form.title"></input>
		</view>

		<view class="cu-form-group align-start">
			<view class="title">{{i18n.msg.content}}</view>
			<textarea maxlength="-1" v-model="form.content" :placeholder="i18n.msg.inputcontent"></textarea>
		</view>

		<!-- <view class="cu-bar bg-white margin-top">
			<view class="action">
				上传图片
			</view>
			<view class="action">
				{{imgList.length}}/4
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-1 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>

				<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view> -->

		<view class="padding flex flex-direction">
			<button class="cu-btn bg-yellow lg" @tap="submit">{{i18n.msg.confirm}}</button>
		</view>


	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex';
	import uQRCode from '@/common/uqrcode.js'
	export default {
		data() {
			return { 
				imgList: [],
				form: {
					title: '', 
					content: '',
				}

			}
		}, 
		computed: { 
			i18n() {
				return this.$t('message');
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
            	title: this.i18n.msg.title
        	});
		},
		onShow() {

		},
		onNavigationBarButtonTap(e) { 
			if (e.index == 0) { 
				uni.navigateTo({
					url: `/pages/user/msglist`
				}) 
			}
		},
		methods: {
			get_object_first(data) {
				for (var key in data)
					return data[key];
			},
			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {

						uni.uploadFile({
							url: this.$host + '/api/common/upload',
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {},
							success: (re) => {
								let rr = JSON.parse(re.data)
								console.log(rr)
								if (rr.code) {
									this.imgList.push(res.tempFilePaths[0]);
									this.form.payimage = rr.data.url;
								}
							},
							error: function(res) {
								console.log(res)
							}
						});
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: this.i18n.popup.tips,
					content: this.i18n.popup.confirm2,
					cancelText: this.i18n.popup.cancel,
					confirmText:this.i18n.popup.confirm,
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1);
							this.form.payimage = '';
						}
					}
				})
			},
			submit() {
				this.$http.post('Message.add', this.form).then(res => {
					this.$api.msg(this.i18n.popup.success);
					// this.getUserPayment()
					setTimeout(() => {
						this.$api.prePage()
					}, 800)
				})
			}
		}
	}
</script>

<style>

</style>
